<?php

namespace App\Model;

use App\BaseModel;
use App\Helpers\Helpers;
use App\User;
use Illuminate\Support\Facades\DB;

class OrderBonus extends BaseModel
{
    protected $table = "order_bonuses";

    protected $hidden = ['openid'];

    //产生分红记录
    public static function createBonus($order)
    {
        if ($order->state != 6 || $order->is_bonus!=0) return false;

        //分红基础金额
        $orderBonusTotal = $order->total_fee;

        $d = [];
        $d['order_id'] = $order->id;
        $d['out_trade_no'] = $order->out_trade_no;
        $d['order_user_id'] = $order->create_user_id;
        $d['order_user_name'] = $order->createUser->name;
        $d['order_user_mobile'] = $order->createUser->mobile;
        $d['order_total'] = $order->total_fee;
        $d['order_bonus_total'] = $orderBonusTotal;

        if (env('AUTO_PAY',0) == 0)  $d['auto_pay'] = 0;

        $scale = config('order.bonus_scale');

        if (!isset($scale[$order->category_id])){
            return false;
        }

        $scale = $scale[$order->category_id];

        $parentDealer = false;
        if ($order->createUser &&  $order->createUser->parent_dealer_id>0 && $order->createUser->active_dealer>0){
            $parentDealer = User::find($order->createUser->parent_dealer_id);
        }

        //经销商分红
        if ($parentDealer){
            $m = new OrderBonus();
            $m->fill($d);
            $m->bonus_user_id = $parentDealer->id;
            $m->bonus_user_name = $parentDealer->name;
            $m->bonus_user_mobile = $parentDealer->mobile;
            $m->order_bonus_scale = $scale['dealer'];
            $m->level = 0;
            $m->amount =  $m->order_bonus_scale*$m->order_bonus_total;
            $m->openid =  $parentDealer->weixinId?$parentDealer->weixinId:'';
            $m->partner_trade_no = Helpers::createSerialNumber('order_bonuses','partner_trade_no', "000");
            $m->desc = $d['order_user_name'].'-'.$d['out_trade_no'];
            $m->save();

            $parentDealer->updateBonusTotal();

            //产生积分记录
            UserPoint::createOneRecord($m->bonus_user_id,$m->amount,$m->desc,'dealer',$m->id);
        }

        $rows = DB::table('t_user_relation')->where('user_id','=',$order->create_user_id)
            ->where('parent_id','<>',$parentDealer->id)
            ->whereIn('level',[1,2,3])->get();




        foreach ($rows as $row){
            $parent = User::find($row->parent_id);
            if ($parent) {
                if (!isset($scale[$row->level])) continue;
                $to = $scale[$row->level]*$d['order_bonus_total'];
                if ($to<0.01) continue;
                $m = new OrderBonus();
                $m->fill($d);
                $m->bonus_user_id = $parent->id;
                $m->bonus_user_name = $parent->name;
                $m->bonus_user_mobile = $parent->mobile;
                $m->order_bonus_scale = $scale[$row->level];
                $m->level = $row->level;
                $m->amount =  $m->order_bonus_scale*$m->order_bonus_total;
                $m->openid =  $parent->weixinId?$parent->weixinId:'';
                $m->partner_trade_no = Helpers::createSerialNumber('order_bonuses','partner_trade_no', $row->level*100);
                $m->desc = $d['order_user_name'].'-'.$d['out_trade_no'];
                $m->save();

                $parent->updateBonusTotal();

                //产生积分记录
                UserPoint::createOneRecord($m->bonus_user_id,$m->amount,$m->desc,'bonus',$m->id);
            }
        }

        $order->is_bonus = 1;
        return $order->save();
    }

}
